package com.example.beixiangsuapp.bxs;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.beixiangsuapp.R;
import com.example.beixiangsuapp.bxs.homepage.HomeFragment;
import com.example.beixiangsuapp.bxs.my.MyFragment;
import com.example.beixiangsuapp.bxs.orderform.OrderformFragment;

public class MainActivity extends FragmentActivity {
    private HomeFragment homeFragment;
    private MyFragment myFragment;
    private OrderformFragment orderformFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private RadioButton home_btn, order_btn, my_btn;
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        setData();
        setListener();
    }

    public void initView() {
        fragmentManager = getSupportFragmentManager();
        homeFragment = (HomeFragment) fragmentManager.findFragmentById(R.id.home_fragment);
        myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.my_fragment);
        orderformFragment = (OrderformFragment) fragmentManager.findFragmentById(R.id.order_form_fragment);
        radioGroup = findViewById(R.id.radioGroup1);
        home_btn = findViewById(R.id.radio0);
        order_btn = findViewById(R.id.radio1);
        my_btn = findViewById(R.id.radio2);
    }

    public void initData() {

    }

    public void setData() {

        fragmentTransaction = fragmentManager.beginTransaction().hide(homeFragment).hide(orderformFragment).hide(myFragment);
        fragmentTransaction.show(homeFragment).commit();
        home_btn.setChecked(true);
    }

    public void setListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                fragmentTransaction = fragmentManager.beginTransaction().hide(homeFragment).hide(orderformFragment).hide(myFragment);
                switch (checkedId) {
                    case R.id.radio0:
                        fragmentTransaction.show(homeFragment).commit();
                        break;
                    case R.id.radio1:
                        fragmentTransaction.show(orderformFragment).commit();
                        break;
                    case R.id.radio2:
                        fragmentTransaction.show(myFragment).commit();
                        break;
                }
            }
        });
    }


}
